﻿using System.Collections.Generic;
using T4UtilityBelt.DataDesigns;
using T4UtilityBelt.Serializers;
using T4UtilityBelt.UxDesigns.Controls;
using T4UtilityBelt.UxDesigns.Screens.Renders;
using T4UtilityBelt.UxDesigns.Specialized.Renderers;

namespace T4UtilityBelt.UxDesigns.Specialized
{
    public class BaseScreen : Screen<BaseScreen, IBaseScreenRenderer>
    {
        private Header _header;
        private StackPanelControl _content;
        private ActionBar _actionBar;

        public BaseScreen()
        {
            StandardConstructor();
        }

        public BaseScreen(string entityName)
        {
            EntityName = entityName;
            Name = entityName;
            StandardConstructor();
        }

        public BaseScreen(string entityName, string screenName)
            
        {
            Name = screenName;
            EntityName = entityName;
            StandardConstructor();
        }

        //public override string Name
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(base.Name))
        //        {
        //            return EntityName;
        //        }
        //        return base.Name;
        //    }
        //    set
        //    {
        //        base.Name = value;
        //    }
        //}

        public string EntityName { get; set; }

        [XIgnore]
        public Entity Entity { get; set; }

        [XIgnore]
        public override ControlCollection Controls
        {
            get
            {
                return base.Controls;
            }
        }

        public override IEnumerable<Control> AllControls()
        {
            if (Controls.Count > 0)
            {
                foreach (Control c in Controls)
                {
                    yield return c;
                    foreach (Control cc in c.AllControls())
                    {
                        yield return cc;
                    }
                }
            }
        }

        private void StandardConstructor()
        {
            Header = new Header() { Text = Name };

            Content = new StackPanelControl();

            ActionBar = new ActionBar();
        }

        internal override void FixModel(T4UtilityBelt.DataDesigns.EntityModel model)
        {
            base.FixModel(model);

            Entity = model.GetEntity(EntityName);
            if (Entity != null)
            {
                Header.FixModel(Entity);

                Content.FixModel(Entity);

                ActionBar.FixModel(Entity);
            }
        }


        public Header Header
        {
            get { return _header; }
            set
            {
                Controls.Switch(_header, value);
                _header = value;
            }
        }

        public StackPanelControl Content
        {
            get { return _content; }
            set
            {
                Controls.Switch(_content, value);
                _content = value;
            }
        }

        public ActionBar ActionBar
        {
            get { return _actionBar; }
            set
            {
                Controls.Switch(_actionBar, value);
                _actionBar = value;
            }
        }
    }
}